package com.lijin.workshop.web.interceptor;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.lijin.workshop.constants.SysConstants;
import com.lijin.workshop.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 用于将页面的请求参数组装成相应的MAP
 * 
 * 查询参数MAP存放在SESSION中
 * 新增和修改参数MAP存放在ACTION的上下文中
 * @author jin
 *
 */
public class ParamMapInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(ParamMapInterceptor.class);

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		ActionContext context = invocation.getInvocationContext();
		Map<String, Object> params = context.getParameters();
		Map<String, Object> session = context.getSession();
		
		// 构造各种参数MAP的KEY，用于从不同的上下文中存取
		String actionName = invocation.getAction().getClass().getName();
		String qryParamMapKey = actionName + SysConstants.QRY_PARAM_MAP_KEY;
		String insertParamMapKey = actionName + SysConstants.QRY_PARAM_MAP_KEY;
		String updateParamMapKey = actionName + SysConstants.QRY_PARAM_MAP_KEY;
		
		Map<String, String> qryParamMap = null;
		Map<String, String> insertParamMap = null;
		Map<String, String> updateParamMap = null;
		
		for (String k : params.keySet()) {
			String[] v = (String[]) params.get(k);
			
			// 将查询参数放入当前Action在session中对应的查询Map
			if (k.startsWith(SysConstants.QRY_PARAM_KEY_PREFFIX)) {
				qryParamMap = (Map<String, String>) session.get(qryParamMapKey);
				if (qryParamMap == null) {
					qryParamMap = new HashMap<String, String>();
					session.put(qryParamMapKey, qryParamMap);
				}
				if (v != null && v.length == 1) {
					String key = k.substring(SysConstants.QRY_PARAM_KEY_PREFFIX_LENGTH);
					qryParamMap.put(key, StringUtil.trim(v[0]));
				}
			}
			
			// 将页面提交的新增请求参数放入Action上下文的新增参数MAP中
			// 新增参数中同一个NAME的参数可能有多个值（如多选框），如果有多个,就用逗号分隔存放
			if (k.startsWith(SysConstants.INSERT_PARAM_KEY_PREFFIX)) {
				insertParamMap = (Map<String, String>) context.get(insertParamMapKey);
				if (insertParamMap == null) {
					insertParamMap = new HashMap<String, String>();
					context.put(insertParamMapKey, insertParamMap);
				}
				String key = k.substring(SysConstants.INSERT_PARAM_KEY_PREFFIX_LENGTH);
				insertParamMap.put(key, StringUtil.StrArrToString(v));
			}
			
			// 将页面提交的修改请求参数放入Action上下文的修改参数MAP中
			// 修改参数中同一个NAME的参数可能有多个值（如多选框）,如果有多个,就用逗号分隔存放
			if (k.startsWith(SysConstants.UPDATE_PARAM_KEY_PREFFIX)) {
				updateParamMap = (Map<String, String>) context.get(updateParamMapKey);
				if (updateParamMap == null) {
					updateParamMap = new HashMap<String, String>();
					context.put(updateParamMapKey, updateParamMap);
				}
				String key = k.substring(SysConstants.UPDATE_PARAM_KEY_PREFFIX_LENGTH);
				updateParamMap.put(key, StringUtil.StrArrToString(v));
			}
		}
		
		// 在调试模式下，打印出各类参数MAP的信息
		if (logger.isDebugEnabled()) {
			logger.debug("qryParamMap-->" + qryParamMap);
			logger.debug("insertParamMap-->" + insertParamMap);
			logger.debug("updateParamMap-->" + updateParamMap);
		}
		
		return invocation.invoke();
	}

}
